package com.erainm.customdeserializer;

import com.erainm.customserializer.User;
import org.apache.kafka.common.serialization.Deserializer;

import java.nio.ByteBuffer;
import java.util.Map;

/**
 * Created by erainm on 2021/9/10 11:21 上午.
 *
 * @Description TODO:自定义反序列化
 **/
public class UserDeserializer implements Deserializer<User> {
    @Override
    public void configure(Map<String, ?> configs, boolean isKey) {

    }

    @Override
    public User deserialize(String topic, byte[] data) {
        ByteBuffer allocate = ByteBuffer.allocate(data.length);
        allocate.put(data);
        allocate.flip();// 反转
        int userId = allocate.getInt();
        int lenght = allocate.getInt();
        System.out.println(lenght);
        String username = new String(data, 8, lenght);// 从8开始截取，截取lenght长度
        return new User(userId,username);
    }

    @Override
    public void close() {

    }
}
